package com.weilive.api.service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.weilive.api.gate.json.protocol.CSA_DiscoverView;
import com.weilive.api.gate.json.protocol.DiscoverModel;
import com.weilive.api.gate.json.protocol.DiscoverUserInfoModel;
import com.weilive.api.gate.json.protocol.RoomInfoModel;
import com.weilive.core.cache.redis.RedisConfig;
import com.weilive.core.cache.redis.RedisService;
import com.weilive.core.utils.Utils;

@Service
public class DiscoverSystem {
	@Autowired
	private RedisService redis;
	// @Autowired
	// private ResourceSystem resourceSystem;
	// @Autowired
	// private UserSystem userSystem;
	@Autowired
	private DataPool dataPool;

	public CSA_DiscoverView getDiscoverMsg(long userId) {
		CSA_DiscoverView view = new CSA_DiscoverView();

		// 固定内容
		LinkedHashMap<Integer, List<RoomInfoModel>> discoverList = dataPool.getDiscoverShowList();
		// 显示列表
		List<DiscoverModel> showList = new ArrayList<DiscoverModel>();

		String fansKey = null;
		if (userId > 0) {
			fansKey = RedisConfig.USER_ME_FANS_PREF + userId;
		}
		for (Map.Entry<Integer, List<RoomInfoModel>> entry : discoverList.entrySet()) {
			int vjTypeId = entry.getKey();
			List<RoomInfoModel> roomInfos = entry.getValue();

			DiscoverModel discoveModel = new DiscoverModel();
			discoveModel.setVjTypeId(vjTypeId);
			discoveModel.setPage(0);

			List<DiscoverUserInfoModel> userList = new ArrayList<DiscoverUserInfoModel>();
			discoveModel.setUserList(userList);
			for (RoomInfoModel roomInfo : roomInfos) {
				DiscoverUserInfoModel user = new DiscoverUserInfoModel();

				user.setIsFans(0);
				if (fansKey != null) {
					user.setIsFans(Utils.isFans(redis.exists(fansKey, String.valueOf(roomInfo.getUserId()))));
				}
				user.setUserInfo(roomInfo);

				userList.add(user);
			}
			// 加入
			showList.add(discoveModel);
		}

		view.setDisList(showList);

		return view;
	}

	/**
	 * 发现更多找前100
	 * 
	 * @param userId
	 * @param vjTypeId
	 * @param page
	 * @return
	 */
	public CSA_DiscoverView getMoveDiscoverMsg(long userId, int vjTypeId, int page) {
		CSA_DiscoverView view = new CSA_DiscoverView();

		if (page < 0) {
			page = 0;
		}
		List<RoomInfoModel> discoverList = dataPool.getDiscoverList(vjTypeId, page);

		String fansKey = null;
		if (userId > 0) {
			fansKey = RedisConfig.USER_ME_FANS_PREF + userId;
		}

		List<DiscoverModel> showList = new ArrayList<DiscoverModel>();

		DiscoverModel discoverModel = new DiscoverModel();
		discoverModel.setVjTypeId(vjTypeId);
		discoverModel.setPage(page);

		List<DiscoverUserInfoModel> userList = new ArrayList<DiscoverUserInfoModel>();
		discoverModel.setUserList(userList);

		for (RoomInfoModel roomInfo : discoverList) {

			DiscoverUserInfoModel user = new DiscoverUserInfoModel();
			user.setIsFans(0);
			if (fansKey != null) {
				user.setIsFans(Utils.isFans(redis.exists(fansKey, String.valueOf(roomInfo.getUserId()))));
			}
			user.setUserInfo(roomInfo);
			userList.add(user);
		}

		showList.add(discoverModel);
		view.setDisList(showList);
		return view;
	}
}
